package com.yjy.service;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yjy.entity.Account;
import com.yjy.entity.Notice;
import com.yjy.exception.CustomerException;
import com.yjy.mapper.NoticeMapper;
import com.yjy.utils.TokenUtils;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class NoticeService {
    @Resource
    NoticeMapper noticeMapper;

    public String notice(String name) {
        if("notice".equals(name)){
            return "notice";
        }else{
//            return "账号错误";//自定义异常
            throw new CustomerException("账号错误!");
        }
    }

    public List<Notice> selectAll(Notice notice){
        return noticeMapper.selectAll(notice);
    }

    public PageInfo<Notice> selectPage(Integer pageNum, Integer pageSize,Notice notice) {
        //开启分页查询
        PageHelper.startPage(pageNum,pageSize);
        List<Notice> notices = noticeMapper.selectAll(notice);
        return PageInfo.of(notices);
    }

    public void add(Notice notice) {
        //获取当前的用户
        Account currentUser = TokenUtils.getCurrentUser();
        if("USER".equals(currentUser.getRole())){
            throw new CustomerException("500","您的角色暂无权限执行该操作");
        }
        notice.setTime(DateUtil.now());
        noticeMapper.insert(notice);
    }

    public void update(Notice notice) {
        //获取当前的用户
        Account currentUser = TokenUtils.getCurrentUser();
        if("USER".equals(currentUser.getRole())){
            throw new CustomerException("500","您的角色暂无权限执行该操作");
        }
        noticeMapper.updateByPrimaryKey(notice);
    }

    public void deleteById(Integer id) {
        //获取当前的用户
        Account currentUser = TokenUtils.getCurrentUser();
        if("USER".equals(currentUser.getRole())){
            throw new CustomerException("500","您的角色暂无权限执行该操作");
        }
        noticeMapper.deleteByPrimaryKey(id);
    }

}






















